package yiran.stock.command;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import yiran.stock.Stock;
import yiran.stock.StockCommand;
import yiran.stock.StockManager;
import yiran.stock.gui.StockGUI;
import yiran.stock.lang.LangHelper;

import java.util.List;

public class ViewCommand implements StockSubCommand {
    private final Stock plugin;

    public ViewCommand(Stock plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean execute(CommandSender sender, String[] args, Stock plugin, StockManager stockManager) {
        if (args.length == 0) {
            sender.sendMessage(LangHelper.getString("stocks.list-title"));
            stockManager.getStockPrices().forEach((symbol, price) -> {
                if (price > 0 && stockManager.isStockLoaded(symbol)) {
                    String lowerSymbol = symbol.toLowerCase();
                    String name = stockManager.getStockName(lowerSymbol);
                    sender.sendMessage(LangHelper.getString("stocks.item-format", name, lowerSymbol, price));
                }
            });
        } else if (args.length == 1) {
            String inputSymbol = args[0];
            String symbol = inputSymbol.toUpperCase();

            boolean isNewStock = !stockManager.getStockPrices().containsKey(symbol.toLowerCase());
            if (!stockManager.loadStockIfNeeded(symbol)) {
                Double price = stockManager.getStockPrice(symbol);
                if (price == null || price <= 0) {
                    sender.sendMessage(LangHelper.getString("errors.stock-not-found", inputSymbol));
                    return true;
                }
            }

            if (isNewStock && stockManager.isStockLoaded(symbol.toLowerCase())) {
                stockManager.addStockToMonitoring(symbol);
            }

            Double price = stockManager.getStockPrice(symbol);
            if (price == null || price <= 0) {
                sender.sendMessage(LangHelper.getString("errors.stock-not-found", inputSymbol));
                return true;
            }

            String name = stockManager.getStockName(symbol);
            sender.sendMessage(LangHelper.getString("stocks.detail-title", name, symbol));
            sender.sendMessage(LangHelper.getString("stocks.detail-price", price));
            sender.sendMessage(LangHelper.getString("stocks.detail-change", stockManager.getChangePercent(symbol)));
        }

        // 打开GUI
        if (!(sender instanceof Player)) {
            sender.sendMessage(LangHelper.getString("errors.only-player-gui"));
            return true;
        }
        Player player = (Player) sender;
        StockGUI gui = new StockGUI(player, stockManager, plugin);
        StockCommand.openGUIs.put(player.getUniqueId(), gui);
        gui.open();
        return true;
    }

    @Override
    public List<String> tabComplete(CommandSender sender, String[] args, StockManager stockManager) {
        return StockCommand.getStockSymbolCompletions(args, stockManager);
    }


}

